﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MSNPSharp;

namespace TienLen
{
    /// <summary>
    /// Offline player selection room.
    /// </summary>
    public partial class PlayerRoom : Form
    {
        private List<ComboBox> players = new List<ComboBox>();
        private CGLib.IComputer[] computers;

        int selectedPlayers = 1;
        int minPlayers;

        /// <summary>
        /// The player list.
        /// </summary>
        public List<ComboBox> Players
        {
            get { return players; }
        }

        public PlayerRoom(CGLib.IGame game)
        {
            InitializeComponent();
            Util.LoadCursor(this, CursorType.Default);

            minPlayers = game.MinPlayers();

            players.Add(p1);
            p1.Items.Add("Local Player");
            p1.SelectedIndex = 0;

            for (int i = 1; i < game.MaxPlayers(); i++)
            {
                // Set properties of the temporary control
                ComboBox tempBox = new ComboBox();
                tempBox.Location = new Point(p1.Location.X, p1.Location.Y + (players.Count * 29));
                tempBox.Size = p1.Size;
                tempBox.Name = "p" + (players.Count + 1).ToString();
                tempBox.DropDownStyle = ComboBoxStyle.DropDownList;
                tempBox.Font = p1.Font;

                this.Controls.Add(tempBox);
                players.Add(tempBox);
            }

            this.Size = new Size(this.Width, this.Height + (players.Count * 25));

            for (int i = 1; i <= 3; i++)
            {
                players[i].Items.Add("Open");

                // Add the computer players
                computers = game.Computers();

                if (computers != null)
                {
                    for (int j = 0; j < computers.Length; j++)
                        players[i].Items.Add(computers[j].Name());
                }

                players[i].SelectedIndex = 0;
                players[i].SelectedIndexChanged += new EventHandler(tempCBX_SelectedIndexChanged);
            }
        }

        private void tempCBX_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (((ComboBox)sender).SelectedIndex == 0)
                selectedPlayers--;
            else
                selectedPlayers++;
        }

        private void btnStart_Click(object sender, EventArgs e)
        {   
            // If the three players are blank, warn the user.
            if (selectedPlayers <= minPlayers)
                MessageBox.Show("Please select players to play in the game.", "Unable to Begin Game", MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
                DialogResult = DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void PlayerRoom_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (DialogResult == DialogResult.OK)
            {
                // Eliminate the players who are blank (in descending order)
                for (int x = players.Count - 1; x >= 0; x--)
                {
                    if (players[x].Text == "Open")
                        players.Remove(players[x]);
                    else if (players[x].Text != "Local Player")
                        players[x].Tag = Activator.CreateInstance(computers[players[x].SelectedIndex - 1].GetType());
                }
            }
        }
    }
}
